package com.cskaoyan.error;

import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.exception.MySQLDuplicateEntryException;
import org.apache.ibatis.binding.BindingException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author looper
 * @
 **/
@RestControllerAdvice
public class MyError {

    @ExceptionHandler(BindingException.class)
    public BaseRespVo getBindException(){

        BaseRespVo baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(402);
        baseRespVo.setErrmsg("参数校验未通过");
        baseRespVo.setData(null);
        return baseRespVo;
    }

    @ExceptionHandler(UnknownAccountException.class)
    public BaseRespVo getUnknownAccountException(){
        BaseRespVo baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(605);
        baseRespVo.setErrmsg("没有访问权限或帐号信息异常");
        baseRespVo.setData(null);
        return baseRespVo;
    }

    /**
     * 此方法内处理参数非法异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public BaseRespVo illegalArgsHandler(IllegalArgumentException exception){
        return BaseRespVo.error(401,exception.getMessage());
    }

    /**
     * 此方法处理值重复异常
     */
    @ExceptionHandler(DuplicateKeyException.class)
    public BaseRespVo duplicateKeyHandler(DuplicateKeyException exception){
        return BaseRespVo.error(602,exception.getMessage());
    }

    /**
     * 此方法处理权限异常
     */
    @ExceptionHandler(AuthorizationException.class)
    public BaseRespVo authorizationExceptionHnadler(){
        return BaseRespVo.error(401,"无访问权限,请联系管理员授权");
    }


    /**
     * 此方法处理凭证错误异常
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    public BaseRespVo incorrectCredentialsExceptionHandler(){
        return BaseRespVo.error(605,"用户名或密码错误");
    }


    /**
     * 此方法处理数据重复插入异常
     */
    @ExceptionHandler(MySQLDuplicateEntryException.class)
    public BaseRespVo MySQLExceptionHandler(){
        return BaseRespVo.error(640,"系统内部错误,请联系管理员");
    }




}
